<style>
	.block-mb15{
		display: inline-block;margin-bottom: 15px;
	}
</style>
<div class="container-fluid" style="padding: 20px 10px 0px 10px">
	<div class="row-fluid">
		<div class="well">
		    <ul class="nav nav-tabs">
		      <li class="active"><b>新建盘点</b></li>
		    </ul>
		    <div id="myTabContent" class="tab-content">
				<div class="tab-pane active in" id="home">
					<form id="formid" action="/drug/addCheck" method="post">
						<span class="block-mb15">所属仓库：
								<select name="warehouse" id="warehouse" style="width:215px;margin-left: 2px;" >
									<option value="">请选择仓库</option>
									<?php if($warehouse){foreach($warehouse as $k1=>$v1){?>
									<option value="<?=$v1['warehouse_id']?>" ><?=$v1['warehouse_name']?></option>
									<?php }}?>
								</select>
						</span>
						<span class="block-mb15" style="margin-left: 10px;">名称：
								<select name="drug_id" id="drug_id" style="width:215px;" >
									<option value="">请选择药品</option>
									<!--<?php if($drug_name){foreach($drug_name as $k2=>$v2){?>-->
									<!--<option value="<?=$v2?>" ><?=$v2?></option>-->
									<!--<?php }}?>-->
								</select>
						</span><br/>

						<span class="block-mb15" style="margin-left: 29px;">种类：<input type="text" readonly value="<?=$getData['type']?>" placeholder="种类" style="width:200px;margin-left: 5px;" name="type" class="type">
						</span>
						<span class="block-mb15" style="margin-left: 10px;">编号：<input type="text" readonly value="<?=$getData['drug_no']?>" placeholder="编号" style="width:200px;margin-left: 5px;" name="drug_no" class="drug_no">
						</span><br/>
						<span class="block-mb15" style="margin-left: 2px;">规格型号：<input type="text" readonly value="<?=$getData['drug_size']?>" placeholder="规格型号" style="width:200px;margin-left: 5px;" name="drug_size" class="drug_size">
						</span>
						<span style="margin-left:10px;">单位：<input type="text"  value="<?=$getData['drug_unit']?>" readonly placeholder="单位" style="width:200px;margin-left: 5px;" name="drug_unit" class="drug_unit">
						</span><br/>
						<span class="block-mb15" style="margin-left: 2px;">库存余量：<input type="text" readonly value="<?=$getData['stock_num']?>" placeholder="库存数量" style="width:200px;margin-left: 5px;" name="stock_num" class="stock_num">
						</span>
						<span class="block-mb15" style="margin-left: 10px;">盘点数量：<input type="text" value="<?=$getData['check_num']?>" placeholder="盘点数量" style="width:177px" name="check_num" class="check_num">
						</span><br/>
						<span class="block-mb15" style="margin-left:15px;">缺失量：<input type="text" readonly value="<?=$getData['lost_num']?>" placeholder="缺失量" style="width:200px;margin-left: 7px;" name="lost_num" class="lost_num">
						</span>
						<span class="block-mb15" style="margin-left:10px;">盘点人：<input type="text" value="<?=$getData['check_user']?>" placeholder="盘点人" style="width:190px;" name="check_user" class="check_user">
						</span><br/>
						<span class="block-mb15" style="margin-left:30px;">备注：<textarea id="textarea" style="width:475px;height:100px;margin-top:-15px;margin-left: 48px;resize: none" name="remark"><?=htmlspecialchars_decode($getData['remark'])?></textarea></span>
						<input type="hidden" value="<?=$getData['id']?>" name="id">
						<input type="hidden" value="1" name="dosubmit" />
					</form>
				</div>
				<div class="btn-toolbar">
				    <button class="btn btn-primary" id="btn-save" style="margin:18px 0 0 70px"><i class="icon-save"></i> 保存</button>
				  <div class="btn-group">
				  </div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
$(function(){
	$('#btn-save').click(function(){
        var check_num = $('input[name=check_num]').val();
        var drug_no = $('input[name=drug_no]').val();
		if($('#warehouse option:selected').val() == ''){
			showmsg('请选择仓库！');
			return false;
		}else if($('#drug_id option:selected').val() == ''){
			showmsg('请选择药品名称！');
			return false;
		}else if(!drug_no){
            showmsg('盘点信息有误！');
            return false;
        }else if(!checkNum(check_num)){
            showmsg('请填写正确的盘点数量');
            return false;
        }else if($('input[name="check_user"]').val() == ''){
			showmsg('请填写盘点人！');
			return false;
		}else{
			setBtn($('#btn-save'));
			$('#formid').submit();
		}
	});

	/*
	* 仓库变动时 获取药品
	* */
	$('#warehouse').on('change',function(){
	    var wval = $('#warehouse option:selected').val();
		if(wval){
            $.ajax({
                url: '/take/onehouse_drug',
                type: 'POST',
                dataType: 'json',
                data: {sel_house: wval},
            })
                .done(function(data) {
                    if(data.code == '0'){
                        if(data.code == '0'){
                            delInputVal();
                            $('#drug_id').empty();
                            $('#drug_id').append('<option value="">请选择药品</option>');
                            for(item in data.data){
                                $('#drug_id').append('<option value="'+item+'">'+data.data[item]+'</option>');
                            }
                        }else{
                            showmsg(data.msg);
                        }
                    }else{
                        showmsg(data.msg);
                    }
                })
                .fail(function() {
                    console.log("error");
                })
        }else{
		    showmsg('请选择正确的仓库');
            delInputVal();
            $('#drug_id').empty();
            $('#drug_id').append('<option value="">请选择药品</option>');
		}
	});

	/*
	* 药品变动时 获取对应的药品信息
	* */
	$('#drug_id').on('change',function(){
        var dval = $(this).val();
        var wval = $('#warehouse').val();
        delInputVal();
        if(dval && wval){
            $.ajax({
                url: '/drug/getCheckInfo',
                type: 'POST',
                dataType: 'json',
                data: {id: dval,warehouse:wval},
            })
                .done(function(data) {
                    if(data.code == '0'){
                        $('input[name=type]').val(data.data.type);
                        $('input[name=drug_no]').val(data.data.drug_no);
                        $('input[name=drug_size]').val(data.data.drug_size);
                        $('input[name=drug_unit]').val(data.data.drug_unit);
                        $('input[name=stock_num]').val(data.data.stock_num);
                        $('input[name=lost_num]').val('');
                    }else{
                        showmsg(data.msg);
                    }
                })
                .fail(function() {
                    console.log("error");
                })
        }
	});

	$('.check_num').on('keyup',function (e) {
        $('input[name=lost_num]').val('');
		var val = $(this).val();
		var stock_num = $('input[name=stock_num]').val();
		if(checkNum(val)){
		    $('input[name=lost_num]').val(stock_num-val);
		}else{
		    showmsg('请输入正确的盘点数量');
		    return false;
		}
    });

    /**
	 * 检测输入的盘点数量是否为整数
     * @param num
     * @returns {boolean}
     */
    function checkNum(num)
    {
        var reg = /^\d+(?=\.{0,1}\d+$|$)/
        if(reg.test(num)) {
            return true;
        }
        $('input[name=check_num]').val('');
        return false ;
    }

    function delInputVal()
	{
        $('input[name=type]').val('');
        $('input[name=drug_no]').val('');
        $('input[name=drug_size]').val('');
        $('input[name=drug_unit]').val('');
        $('input[name=stock_num]').val('');
        $('input[name=lost_num]').val('');
        $('input[name=check_num]').val('');
        $('input[name=check_user]').val('');
        $('#textarea').val('');
	}

})
</script>